package com.tm.servicemsm.demos;

/**
 * 2024/7/20
 * 实现用户发起提现，输入提现金额，根据提现规则扣除相应的手续费完成提现
 * Cjj
 */
public class User {
    private int id;
    private String username;
    private double balance;//用户余额

    //假设的数据库操作方法
    public void debit(double amount){
        this.balance-=amount;
        //这里应该有数据库操作来更新用户额
    }

    public void credit(double amount){
        this.balance+=amount;
        //这里应该有数据库操作来更新用户额
    }

    public class WithdrawalService{
         public void withdraw(User user,double withdrawalAmount) throws InstantiationException {
             //提现规则：手续费率0.1%
             double handlingFee = withdrawalAmount * 0.001;
             double netAmount = withdrawalAmount - handlingFee;

             //检查用户余额是否足够
             if(user.getBalance()<withdrawalAmount){
                 throw new InstantiationException("User balance is insufficient");
             }

             //扣除手续费并完成提现
             user.debit(withdrawalAmount);
             user.credit(netAmount);
         }
    }

    private double getBalance() {
        return 0.001;
    }

    public class InsufficientFundsException extends RuntimeException{
         public InsufficientFundsException(String message){
             super(message);
         }
    }
}
